package com.asiainfo.sqlextractor.adapter;

import com.asiainfo.sqlextractor.constant.Def;
import com.asiainfo.sqlextractor.util.JavaTypeResolve;
import org.apache.calcite.schema.Schema;
import org.apache.calcite.schema.SchemaFactory;
import org.apache.calcite.schema.SchemaPlus;
import org.apache.calcite.util.Pair;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public final class MemSchemaFactory implements SchemaFactory {

    public Schema create(final SchemaPlus parentSchema, final String name, final Map<String, Object> operand) {
        {
            Map<String, String> columns = Def.columns1;
            List<Pair<String, Class<?>>> nameTypes = columns.entrySet().stream().map(entry -> {
                String column = entry.getKey();
                Class<?> clz = JavaTypeResolve.resolveJavaType(entry.getValue());
                return Pair.<String, Class<?>>of(column, clz);
            }).collect(Collectors.toList());
            TableDefine tableDefine = new TableDefine("MOT_ORDER", nameTypes);
            parentSchema.add(tableDefine.name(), new MemTable(tableDefine));
        }

        {
            Map<String, String> columns = Def.columns2;
            List<Pair<String, Class<?>>> nameTypes = columns.entrySet().stream().map(entry -> {
                String column = entry.getKey();
                Class<?> clz = JavaTypeResolve.resolveJavaType(entry.getValue());
                return Pair.<String, Class<?>>of(column, clz);
            }).collect(Collectors.toList());
            TableDefine tableDefine = new TableDefine("ORDER_ITEM", nameTypes);
            parentSchema.add(tableDefine.name(), new MemTable(tableDefine));
        }
        return parentSchema;
    }

    public void doParser() {

    }

}
